package sse.ustc.cc.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import sse.ustc.cc.custom.AddCourseTimeView;
import sse.ustc.cc.util.AddCourseInfo;
import sse.ustc.cc.util.ClassInfo;
import sse.ustc.cc.util.CourseTime;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * 课程详细activity
 * 
 * @author xuhao
 * 
 */
@SuppressLint("NewApi")
public class CourseDetailAty extends Activity {
	private static String TAG = "CourseDetailActivity";
	private EditText courseName = null;// 课程名称
	private EditText courseRoom = null;// 课程地点
	private EditText courseteacher = null;// 课程老师名字

	private Button readdtime = null;// 添加其他时间段按钮
	private Button addcourseok = null;// 确定添加课程按钮
	private LinearLayout liner;
	// 课程信息对象
	public AddCourseInfo addcourseInfo = new AddCourseInfo();
	// 课程时间list
	private List<AddCourseTimeView> addtimeList = new ArrayList<AddCourseTimeView>();
	// 在该activity的顶部显示actionbar，主要用于显示编辑按钮

	private ArrayList<ClassInfo> classInfoList;
	private HashMap sectionWeek = new HashMap() {
		{
			put(1, "周一");
			put(2, "周二");
			put(3, "周三");
			put(4, "周四");
			put(5, "周五");
			put(6, "周六");
			put(7, "周日");
		}
	};
	ActionBar actionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_edit_schedule);
		// 显示actionbar
		actionBar = getActionBar();
		actionBar.show();

		courseName = (EditText) findViewById(R.id.course_name);
		courseRoom = (EditText) findViewById(R.id.course_room);
		courseteacher = (EditText) findViewById(R.id.course_teacher);
		readdtime = (Button) findViewById(R.id.readdtime);
		addcourseok = (Button) findViewById(R.id.addcourseok);
		liner = (LinearLayout) findViewById(R.id.liner);
		/**
		 * 预处理： 1、得到需要显示其课程详细信息的对象 2、根据其课程时间段初始化对应的AddCourseTime 3、初始化显示内容
		 * 4、设置AddCourseTime对象中的控件为不可用状态
		 */
		Intent intent = getIntent();
		classInfoList = (ArrayList<ClassInfo>) intent
				.getSerializableExtra("courseinfo");

		// 设置显示内容
		courseName.setText(classInfoList.get(0).getClassname());
		courseRoom.setText(classInfoList.get(0).getClassRoom());
		courseteacher.setText(classInfoList.get(0).getTeacher_name());

		courseName.setEnabled(false);
		courseRoom.setEnabled(false);
		courseteacher.setEnabled(false);
		// 该课程的时间段数
		final int courseCount = classInfoList.size();
		final int indexStart = liner.getChildCount()-1;
		// 将课程详细信息显示到界面中
		for (int i = 0; i < courseCount; i++) {
			LinearLayout liner0 = new LinearLayout(this);
			AddCourseTimeView addtime = new AddCourseTimeView(this);
			Button delTime = new Button(this);
			delTime.setText("删除");
			delTime.setOnClickListener(new DelTime());
			liner0.addView(delTime);
			liner0.addView(addtime);
			int num = liner.getChildCount()-1;
			liner.addView(liner0, num);
			addtimeList.add(addtime);
			
			String week = "";
			if( 0 != classInfoList.get(i).getWeekday()) {
				week = sectionWeek.get(classInfoList.get(i).getWeekday()).toString();
			}
			
			addtime.course_week.setText(classInfoList.get(i).getWeek_begin()
					+ " ~ " + classInfoList.get(i).getWeek_end() + "周");
			addtime.course_jieshu.setText(week + "  "
					+ classInfoList.get(i).getSection_begin() + " ~ "
					+ classInfoList.get(i).getSection_end() + "节");
			addtime.course_time.setText(classInfoList.get(i)
					.getCourse_begin_time() + " ~ "
					+ classInfoList.get(i).getCourse_end_time());

			CourseTime temp = new CourseTime();
			Log.i(TAG, ">>>>>>>>>>>>>>>>>>>Week_begin"
					+ classInfoList.get(i).getWeek_begin());
			if (null != classInfoList.get(i).getWeek_begin()) {
				temp.setStartWeek(Integer.parseInt(classInfoList.get(i)
						.getWeek_begin()));
			}
			if (null != classInfoList.get(i).getWeek_end()) {
				temp.setEndWeek(Integer.parseInt(classInfoList.get(i)
						.getWeek_end()));
			}
			if (null != classInfoList.get(i).getSection_begin()) {
				temp.setStartJieShu(Integer.parseInt(classInfoList.get(i)
						.getSection_begin()));
			}
			if (null != classInfoList.get(i).getSection_end()) {
				temp.setEndJieShu(Integer.parseInt(classInfoList.get(i)
						.getSection_end()));
			}
			if (null != classInfoList.get(i).getCourse_begin_time()) {
				temp.setStartTime(classInfoList.get(i).getCourse_begin_time());
			}
			if (null != classInfoList.get(i).getCourse_end_time()) {
				temp.setEndTime(classInfoList.get(i).getCourse_end_time());
			}
			temp.setWeekJieShu(week);

			addtime.course_jieshu.setEnabled(false);
			addtime.course_time.setEnabled(false);
			addtime.course_week.setEnabled(false);
			addtime.setCourseTime(temp);
			
		}
		// 添加其他时间段响应事件
		readdtime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 最多只能添加五个时间段
				// 点击添加其他时间段按钮，添加时间段
				LinearLayout liner0 = new LinearLayout(CourseDetailAty.this);
				AddCourseTimeView addtime = new AddCourseTimeView(CourseDetailAty.this);
				Button delTime = new Button(CourseDetailAty.this);
				delTime.setText("删除");
				delTime.setOnClickListener(new DelTime());
				liner0.addView(delTime);
				liner0.addView(addtime);
				int num = liner.getChildCount();
				liner.addView(liner0, num-1);
			}

		});
		
		/**
		 * 确定添加课程
		 */
		addcourseok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 将课程信息添加到addcourseInfo
				addcourseInfo.setCourseName(courseName.getText().toString());
				addcourseInfo.setCourseRoom(courseRoom.getText().toString());
				addcourseInfo.setCourseTeacher(courseteacher.getText()
						.toString());
				Log.e("",">>>>>>>>>>>>>>>>>>>>>>>>CourseName");
				Log.e("CourseName", courseName.getText().toString());
				Log.e("CourseRoom", courseName.getText().toString());
				Log.e("CourseTeacher", courseName.getText().toString());
				// 清空以前的数据
				addcourseInfo.getCourseTimeList().clear();
				
				int num = liner.getChildCount();
				for(int i = indexStart;i<num-1;i++){
					LinearLayout l = (LinearLayout) liner.getChildAt(i);
					addtimeList.add((AddCourseTimeView) l.getChildAt(1));
				}
				Log.v(TAG, "addtimeList.size()1" + addtimeList.size());
				
				// 重新加载课程时间段数据
				for (int i = 0; i < addtimeList.size()
						&& addtimeList.get(i).getCourseTime() != null; i++) {
					if (addtimeList.get(i).getCourseTime() != null) {
						addcourseInfo.getCourseTimeList().add(
								addtimeList.get(i).getCourseTime());
					}
				}
				// 输出
				// for (int i = 0; i < addcourseInfo.getCourseTimeList().size();
				// i++) {
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getStartWeek()
				// + "");
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getEndWeek()
				// + "");
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getWeekJieShu());
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getStartJieShu()
				// + "");
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getEndJieShu()
				// + "");
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getStartTime());
				// Log.v(TAG, addcourseInfo.getCourseTimeList().get(i)
				// .getEndTime());
				// }
			}
		});
	}
	class DelTime implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			liner.removeView((View) arg0.getParent());
		}
		
	} 
	/**
	 * 添加actionbar上的按钮--编辑
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		// 添加菜单项
		MenuItem edit = menu.add(0, 0, 0, "编辑");
		// 绑定到ActionBar
		edit.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		/**
		 * 添加响应事件，将课程详细中的信息设置为可编辑状态
		 */
		edit.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				// TODO Auto-generated method stub
				courseName.setEnabled(true);
				courseRoom.setEnabled(true);
				courseteacher.setEnabled(true);

				for (int i = 0; i < classInfoList.size(); i++) {
					addtimeList.get(i).course_jieshu.setEnabled(true);
					addtimeList.get(i).course_time.setEnabled(true);
					addtimeList.get(i).course_week.setEnabled(true);
				}

				return false;
			}
		});

		return true;
	}

}
